package com.cn.kabalifemerchants.ui.page.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.callback.SchedulerTransformer;
import com.cn.kabalifemerchants.config.CustomRefreshListener;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.RefreshHelper;
import com.cn.kabalifemerchants.event.RxBus;
import com.cn.kabalifemerchants.event.RxBusBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.page.adapter.ServiceCaseListAdapter;
import com.cn.kabalifemerchants.ui.page.bean.CaseDetailsBean;
import com.cn.kabalifemerchants.ui.page.bean.CaseListBean;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.view.NotDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

/**
 * Created by 晓林 on 2020/9/11.
 * function: 服务案例
 */
public class ServiceCaseListActivity extends IBaseActivity<ServiceCaseListView, ServiceCaseListPresenter> implements ServiceCaseListView {


    @BindView(R.id.rv_notice)
    RecyclerView rvNotice;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private View notDataView;
    List<CaseListBean.ResultBean.DataBean> mList;
    ServiceCaseListAdapter mAdapter;

    Bundle bundle;
    int id;

    @Override
    protected ServiceCaseListPresenter createPresenter() {
        return new ServiceCaseListPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_service_case;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        bundle = getIntent().getExtras();
        id = bundle.getInt("id");
        mList = new ArrayList<>();
        rvNotice.setLayoutManager(new GridLayoutManager(getMe(), 2));
        rvNotice.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ServiceCaseListAdapter(R.layout.activity_service_case_item, mList);
        //给RecyclerView设置适配器
        rvNotice.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(this, rvNotice, refreshLayout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo 服务案例详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                $startActivity(ServiceCaseDetailsActivity.class, bundle);
            }
        });
        pagehttp = 1;
        getListData();

        rxBusSubscription = RxBus.getDefault().toObservable(RxBusBean.class)
                .compose(new SchedulerTransformer<RxBusBean>())
                .subscribe(new Action1<RxBusBean>() {
                    @SuppressLint("WrongConstant")
                    @Override
                    public void call(RxBusBean rxBusBean) {
                        if (rxBusBean.getId() == 20001 && rxBusBean.getSign() == 20001) {
                            refreshLayout.autoRefresh();
                        }

                    }
                });
    }

    @Override
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {
        mPresenter.onCaseListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum, id);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }


    @Override
    public void onCaseListSuccess(CaseListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }

    @Override
    public void onCaseDetailsSuccess(CaseDetailsBean bean) {

    }

    @Override
    public void onCaseSetUpSuccess(MsgBean bean) {

    }


    @OnClick({R.id.ll_btn_left, R.id.ll_btn_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_btn_left:
                finish();
                break;
            case R.id.ll_btn_right:
                break;
        }
    }
}

